/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.supplier.modular.notice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.supplier.modular.notice.entity.SupplierNotice;
import vip.xiaonuo.supplier.modular.notice.enums.SupplierNoticeStatusEnum;
import vip.xiaonuo.supplier.modular.notice.mapper.SupplierNoticeMapper;
import vip.xiaonuo.supplier.modular.notice.param.SupplierNoticeAddParam;
import vip.xiaonuo.supplier.modular.notice.param.SupplierNoticeEditParam;
import vip.xiaonuo.supplier.modular.notice.param.SupplierNoticeIdParam;
import vip.xiaonuo.supplier.modular.notice.param.SupplierNoticePageParam;
import vip.xiaonuo.supplier.modular.notice.service.SupplierNoticeService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

import java.util.List;

/**
 * 通知公告Service接口实现类
 *
 * @author yubaoshan
 * @date  2024/07/11 14:46
 **/
@Service
public class SupplierNoticeServiceImpl extends ServiceImpl<SupplierNoticeMapper, SupplierNotice> implements SupplierNoticeService {


    @Override
    public Page<SupplierNotice> page(SupplierNoticePageParam supplierNoticePageParam) {
        QueryWrapper<SupplierNotice> queryWrapper = new QueryWrapper<SupplierNotice>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierNoticePageParam.getTitle())) {
            queryWrapper.lambda().like(SupplierNotice::getTitle, supplierNoticePageParam.getTitle());
        }
        if(ObjectUtil.isNotEmpty(supplierNoticePageParam.getType())) {
            queryWrapper.lambda().eq(SupplierNotice::getType, supplierNoticePageParam.getType());
        }
        if(ObjectUtil.isNotEmpty(supplierNoticePageParam.getPlace())) {
            queryWrapper.lambda().like(SupplierNotice::getPlace, supplierNoticePageParam.getPlace());
        }
        if(ObjectUtil.isNotEmpty(supplierNoticePageParam.getStatus())) {
            queryWrapper.lambda().eq(SupplierNotice::getStatus, supplierNoticePageParam.getStatus());
        }
        if(ObjectUtil.isNotEmpty(supplierNoticePageParam.getStartCreateTime()) && ObjectUtil.isNotEmpty(supplierNoticePageParam.getEndCreateTime())) {
            queryWrapper.lambda().between(SupplierNotice::getCreateTime, supplierNoticePageParam.getStartCreateTime(), supplierNoticePageParam.getEndCreateTime());
        }
        if(ObjectUtil.isAllNotEmpty(supplierNoticePageParam.getSortField(), supplierNoticePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierNoticePageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierNoticePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierNoticePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierNotice::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierNoticeAddParam supplierNoticeAddParam) {
        SupplierNotice supplierNotice = BeanUtil.toBean(supplierNoticeAddParam, SupplierNotice.class);
        // 默认为禁用的
        supplierNotice.setStatus(SupplierNoticeStatusEnum.DISABLE.getValue());
        this.save(supplierNotice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierNoticeEditParam supplierNoticeEditParam) {
        SupplierNotice supplierNotice = this.queryEntity(supplierNoticeEditParam.getId());
        BeanUtil.copyProperties(supplierNoticeEditParam, supplierNotice);
        this.updateById(supplierNotice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierNoticeIdParam> supplierNoticeIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierNoticeIdParamList, SupplierNoticeIdParam::getId));
    }

    @Override
    public SupplierNotice detail(SupplierNoticeIdParam supplierNoticeIdParam) {
        return this.queryEntity(supplierNoticeIdParam.getId());
    }

    @Override
    public SupplierNotice queryEntity(String id) {
        SupplierNotice supplierNotice = this.getById(id);
        if(ObjectUtil.isEmpty(supplierNotice)) {
            throw new CommonException("通知公告不存在，id值为：{}", id);
        }
        return supplierNotice;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void disableStatus(SupplierNoticeIdParam supplierNoticeIdParam) {
        this.update(new LambdaUpdateWrapper<SupplierNotice>().eq(SupplierNotice::getId,
                supplierNoticeIdParam.getId()).set(SupplierNotice::getStatus, SupplierNoticeStatusEnum.DISABLE.getValue()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void enableStatus(SupplierNoticeIdParam supplierNoticeIdParam) {
        this.update(new LambdaUpdateWrapper<SupplierNotice>().eq(SupplierNotice::getId,
                supplierNoticeIdParam.getId()).set(SupplierNotice::getStatus, SupplierNoticeStatusEnum.ENABLE.getValue()));
    }
}
